package forces;

import java.awt.Dimension;

import util.Location;
import util.Vector;

/**
 * defines center of mass as an extension of Force.
 * @author Schwartz & Roy
 *
 */
public class CenterOfMassForce extends Force {

    private double magnitude;
    private double exponent;
    private Location location;

    /**
     * constructor sets default center of mass to 0.
     */
    public CenterOfMassForce() {
        this(DEFAULT_MAGNITUDE, DEFAULT_EXPONENT, new Location());
    }

    /**
     * constructor sets center of mass.
     * @param mag is the magnitude of the force
     * @param exp is the exponent of the force
     * @param loc is the location of the center of mass
     */
    public CenterOfMassForce(double mag, double exp, Location loc) {
        super();
        magnitude = mag;
        exponent = exp;
        location = loc;
    }

    /**
     * updates the center of mass's location.
     * @param loc is the new location
     */
    public void updateCenterOfMassLocation(Location loc) {
        location = loc;
    }

    /**
     * @param massLocation is the location of the mass to which
     *      the force is being applied
     * @param bounds of the simulation
     * @return the force vector for center of mass.
     */
    @Override
    public Vector getForceVector(Location massLocation, Dimension bounds) {
        if (isOn()) {
            double r = massLocation.distance(location);
            double force = magnitude / Math.pow(getSafeDistance(r), exponent);
            double angle = Vector.angleBetween(location, massLocation);
            return new Vector(angle, force);
        }
        return new Vector();
    }
}
